package com.mjlf.nio.chat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

public class NioServer {

    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(8013));
        serverSocketChannel.configureBlocking(false);
        SelectionKey sk = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        long count = 1;

        while (true){
            long start = System.nanoTime();
            selector.select(2000);
            long end = System.nanoTime();
            if(end - start >= 2000){
                count = 1;
            }else {
                count ++;
            }

            if(count > 10){
                Selector newSelector = Selector.open();
                Selector oldSelector = selector;
                Set<SelectionKey> selectionKeys = oldSelector.selectedKeys();
                for(SelectionKey key : selectionKeys){
                    key.cancel();
                    key.channel().register(newSelector, key.interestOps());
                }
                oldSelector.close();
                selector = newSelector;
                count = 1;
                selector.selectNow();
                continue;
            }

            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while(iterator.hasNext()){
                SelectionKey key = iterator.next();
                if(key.isValid()){
                    if(key.isAcceptable()){
                        ServerSocketChannel socketChannel = (ServerSocketChannel)key.channel();
                        SocketChannel channel = socketChannel.accept();
                        channel.configureBlocking(false);
                        channel.register(selector, SelectionKey.OP_READ);
                    }else if(key.isReadable()){
                        SocketChannel channel = (SocketChannel) key.channel();

                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                        channel.write(ByteBuffer.wrap("receive:".getBytes()));
                        while (channel.read(byteBuffer) > 0){
                            byteBuffer.flip();
                            channel.write(byteBuffer);
                            System.out.println(new String(byteBuffer.array()));
                            byteBuffer.clear();
                        }
                    }
                }
                selectionKeys.remove(key);
            }
        }
    }
}
